/**
 * ChessSquare.java
 * This class represents each square on the chessboard
 * @author Tyler Eastman
 */
package bbchess.units;

import bbchess.util.ImageLoader;
import java.awt.*;
import java.util.ArrayList;

public class ChessSquare extends javax.swing.JComponent {
    
    // declare variables
    private String position;
    private boolean mouseOver;
    private boolean current;
    private boolean legal;
    private ArrayList<ChessPiece> capturable;
    private boolean threatened;
    private boolean lastMove;
    private boolean enPassantCapturable;
    
    private ChessPiece contents;
    
    public ChessSquare(String position) {

        this.position = position;

        current = false;
        legal = false;
        capturable = new ArrayList<ChessPiece>();
        threatened = false;
        contents = null;
        mouseOver = false;
        enPassantCapturable = false;
    }

    public void reset() {
        current = false;
        legal = false;
        capturable.clear();
        threatened = false;
        contents = null;
        lastMove = false;
        mouseOver = false;
    }

    @Override
    public String toString() {
        return position;
    }
    
    private void drawCurrentSquare(boolean draw, Graphics g) {
        if (draw) {
            g.drawImage(ImageLoader.getCurrentImage(), 0, 0, getWidth(), getHeight(), this);
        }
    }

    private void drawLegalMoveSquare(boolean draw, Graphics g) {
        if (draw) {
            g.drawImage(ImageLoader.getHighlightImage(), 0, 0, getWidth(), getHeight(), this);
        }
    }

    private void drawThreatenedSquare(boolean draw, Graphics g) {
        if (draw) {
            g.drawImage(ImageLoader.getThreatenedImage(), 0, 0, getWidth(), getHeight(), this);
        }
    }

    private void drawLastMoveSquare(boolean draw, Graphics g) {
        if (draw) {
            g.drawImage(ImageLoader.getLastMoveImage(), 0, 0, getWidth(), getHeight(), this);
        }
    }

    @Override
    public void paintComponent( Graphics g )
    {

        // should we highlight the square?
        if (mouseOver) {
            g.drawImage(ImageLoader.getMouseHighlightImage(), 0, 0, getWidth(), getHeight(), this);
        }

        // draw current square?
        drawCurrentSquare(current, g);
        drawLegalMoveSquare(legal, g);
        drawLastMoveSquare(lastMove, g);

        // draw if enpassant captureable
        drawThreatenedSquare(enPassantCapturable, g);


        // draw contents if there are any
        if (contents != null && contents.isVisible()) {
            g.drawImage(contents.getSprite(), 0, 0, getWidth(), getHeight(), this );
            drawThreatenedSquare(contents.getCapturable(), g);
        }
    }

    /**
     * Returns the squares position in string form.  Example "A1"
     * @return the position
     */
    public String getPosition() {
        return position;
    }

    /**
     * Tests if the mouse is over the square and repaints the square if it is.
     * @return
     */
    public boolean testMouseOver() {
        if (getMousePosition() != null) {
            mouseOver = true;
            repaint();
            return true;
        } else {
            if (mouseOver) {
                mouseOver = false;
                repaint();
                return false;
            } else {
                mouseOver = false;
                return false;
            }
        }
    }

    public void setCurrent(boolean choice) {
        current = choice;
    }

    public boolean getCurrent() {
        return current;
    }

    public void setLegal(boolean choice) {
        legal = choice;
    }

    public boolean getLegal() {
        return legal;
    }

    public void addCapturable(ChessPiece piece) {
        capturable.add(piece);
    }

    public ArrayList<ChessPiece> getCapturable() {
        return capturable;
    }

    public void clearCapturable() {
        capturable.clear();
        enPassantCapturable = false;
    }

    public void setThreatened(boolean choice) {
        threatened = choice;
    }

    public boolean getThreatened() {
        return threatened;
    }

    public void setLastMove(boolean choice) {
        lastMove = choice;
    }

    public boolean getLastMove() {
        return lastMove;
    }

    /**
     * @return the contents
     */
    public ChessPiece getContents() {
        return contents;
    }

    /**
     * @param contents the contents to set
     */
    public void setContents(ChessPiece contents) {
        this.contents = contents;
    }

    /**
     * Clears the contents of the square.  Called before any movement calculations
     * are performed.
     */
    public void clearContents() {
        contents = null;
    }

    @Override
    public void paint(Graphics g) {
        paintComponent(g);
    }

    @Override
    public void update(Graphics g) {
        paint(g);
    }

    /**
     * @return the enPassantCapturable
     */
    public boolean isEnPassantCapturable() {
        return enPassantCapturable;
    }

    /**
     * @param enPassantCapturable the enPassantCapturable to set
     */
    public void setEnPassantCapturable(boolean enPassantCapturable) {
        this.enPassantCapturable = enPassantCapturable;
    }
}
